package com.verifone.tony.databasetest;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Select;

import org.junit.Test;
import org.junit.runner.RunWith;

import static com.verifone.tony.databasetest.MyDatabase.VERSION;
import static org.junit.Assert.*;

import com.verifone.tony.databasetest.DBFunction;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private static final String TAG = "------ExampleInstrumentedTest";

    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.verifone.tony.databasetest", appContext.getPackageName());
    }

    @Test
    public void dbFlowAdd() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        //assertEquals("hello", "hello");

        FlowManager.init(new FlowConfig.Builder(appContext).build());
        DBFlowModel dbFlowModel = new DBFlowModel();
        dbFlowModel.id = 1;
        dbFlowModel.setModelname("dbtest1");
        dbFlowModel.setName("Ruomiz");
        dbFlowModel.setAddress("Wuhan");
        dbFlowModel.setAge(100);
        dbFlowModel.save();
    }

    @Test
    public void dbFlowGet() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();

        FlowManager.init(new FlowConfig.Builder(context).build());
        DBFlowModel dbFlowModel = new Select().from(DBFlowModel.class).where(DBFlowModel_Table.modelname.is("dbtest1")).querySingle();
        assertNotNull(dbFlowModel);
        Log.d(TAG, "V=" + VERSION + ", name is " + dbFlowModel.getName());

        assertEquals(dbFlowModel.getModelname(), "dbtest1");
        assertEquals(dbFlowModel.getName(), "Ruomiz");
        assertEquals(dbFlowModel.getAddress(), "Wuhan");
        assertEquals(dbFlowModel.getAge(), 100);
    }

    //测试新接口， DBFunction
    @Test
    public void dbAddTable1() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();
        DBFlowModel dbFlowModel;

        //初始化数据库
        DBFunction.dbInit(context);

        //增
        dbFlowModel = new DBFlowModel();
        dbFlowModel.setModelname("table_1");
        dbFlowModel.setName("Ruomiz");
        dbFlowModel.setAddress("Wuhan");
        dbFlowModel.setAge(100);
        DBFunction.db1Add(dbFlowModel);

        //查
        dbFlowModel = DBFunction.db1Get("table_1");
        assertNotNull(dbFlowModel);
        assertEquals(dbFlowModel.getModelname(), "table_1");
        assertEquals(dbFlowModel.getName(), "Ruomiz");
        assertEquals(dbFlowModel.getAddress(), "Wuhan");
        assertEquals(dbFlowModel.getAge(), 100);

        //改
        dbFlowModel = new DBFlowModel();
        dbFlowModel.setModelname("table_1");
        dbFlowModel.setName("Tony");
        dbFlowModel.setAddress("BeiJing");
        dbFlowModel.setAge(123);
        DBFunction.db1Add(dbFlowModel);

        //查
        dbFlowModel = DBFunction.db1Get("table_1");
        assertNotNull(dbFlowModel);
        assertEquals(dbFlowModel.getModelname(), "table_1");
        assertEquals(dbFlowModel.getName(), "Tony");
        assertEquals(dbFlowModel.getAddress(), "BeiJing");
        assertEquals(dbFlowModel.getAge(), 123);

        //删
        DBFunction.db1Del("table_1");

        //查
        dbFlowModel = DBFunction.db1Get("table_1");
        assertNull(dbFlowModel);
    }
}
